package com.atguigu.udmp.stream.extractor;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.udmp.stream.bean.UserEvent;
import com.atguigu.udmp.stream.bean.UserEventDefine;
import com.atguigu.udmp.stream.common.constant.UdmpConst;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/*
   模板模式中的控制父类
 */
@Slf4j
public abstract class UserEventExtractor {


    public UserEvent extract(JSONObject jsonObject, UserEventDefine userEventDefine) {
        String table= jsonObject.getString("table");

        if((table!=null&&userEventDefine.getEventSource().equals(UdmpConst.USER_EVENT_SOURCE_TYPE_DB) )   //有表名则匹配DB类型 无表名则匹配LOG类型
                || (table==null &&userEventDefine.getEventSource().equals(UdmpConst.USER_EVENT_SOURCE_TYPE_LOG)) ){
            try{
                if(match(jsonObject)){
                    UserEvent userEvent = new UserEvent();
                    userEvent.setEventDefineId(userEventDefine.getId());
                    userEvent.setEventName(userEventDefine.getEventName());
                    userEvent.setUserId( extractUserId(jsonObject ));
                    userEvent.setEventInfoJson( extractEventInfoJson(jsonObject ));
                    userEvent.setEventRecordKey( extractEventRecordKey( jsonObject));
                    userEvent.setDt(extractDt(jsonObject));
                    userEvent.setEventTime(extractEventTime(jsonObject));
                    return userEvent;
                }
            }catch (Exception e){
                log.warn("提取事件有误");
                e.printStackTrace();
            }

        }
        return  null;

    }

    /*
    返回该数据是否匹配本用户事件
     */
    public abstract boolean match(JSONObject dataJsonObj);

    public abstract String extractDt(JSONObject dataJsonObj);
    public abstract String extractEventTime(JSONObject dataJsonObj);
    public abstract Long extractUserId(JSONObject dataJsonObj);
    public abstract String extractEventInfoJson(JSONObject dataJsonObj);
    public abstract String extractEventRecordKey(JSONObject dataJsonObj);



}
